<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
        <title>yvmobile Test</title>
        <link rel="stylesheet" type="text/css" href="css/yvmobile.css" />
    </head>
    <body>
        <!-- yvmobile Network Indicator Widget -->
        <div class='block' id='networkindicatorblock'>
            <div id='network-indicator'>
            </div>
            <div id='sync-indicator'>
                Sync Indicator
            </div>
        </div>
        <form action="javascript:get(document.getElementById(this.name));" name="myform" id="myform">
            <textarea id="mytextarea1">
                ajax post
            </textarea>
            <textarea id="mytextarea2">
                test
            </textarea>
            <br>
            <input type="button" name="button" value="POST Test" onclick="postTest();"/><input type="button" name="button" value="GET Test" onclick="getTest();"/>
        </form><input type="button" name="button" value="Sync Test" onclick="yvmobile.offline.sync();"/>
    </body>
</html>
<script src="gears_init.js" type='text/javascript'>
</script>
<script src="yvmobile.js" type='text/javascript'>
</script>
<script type='text/javascript'>
    //yvmobile.logger.init(false);
    
    
    
    
    if (yvmobile.offline.init("yvmobile", {
        autoRefresh: true,
        autoSync: true,
        autoDiscovery: true,
        networkPollInterval: 30,
        networkCheckUrl: location.href + "network_check.txt",
        networkTimeout: 15,
        timeBetweenSync: 6000
    }, null, true) ==
    true) {
        // JSON TEST	
        yvmobile.logger.info("yvmobile.utils.json.parse TEST");
        var myJSONString = '{"tabla":[{"nombre":"costotipos"}],"argumentos": [{"id": 1, "created": "2008-09-17 09: 27: 13", "description": "","modifies": "2008-09-17 09: 27: 13","title": "RUTA 229 CHOFER 1","usuario_id": 1}]}';
        var myJSONObject = yvmobile.utils.json.parse(myJSONString);
        yvmobile.logger.info(myJSONObject.argumentos[0].id);
        
        //UUID TEST
        yvmobile.logger.info("yvmobile.utils.randomUUID TEST");
        yvmobile.logger.info(yvmobile.utils.randomUUID());
        
        // OnSync TEST
        yvmobile.offline.onSync = function(){
            parameters = "mytextarea1=" + encodeURI(document.getElementById("mytextarea1").value) +
            "&mytextarea2=" +
            encodeURI(document.getElementById("mytextarea2").value);
            yvmobile.ajax.request("http://kaai.com.ve/labs/yvmobile/post.php", function(responseText, success){
                if (success == true) {
                    alert(responseText + "OnSync Success!!!");
                }
            }, "POST", parameters);
        }
        
        // AJAX POST TEST
        function postTest(){
            yvmobile.logger.info("yvmobile.ajax.request POST TEST");
            parameters = "mytextarea1=" + encodeURI(document.getElementById("mytextarea1").value) +
            "&mytextarea2=" +
            encodeURI(document.getElementById("mytextarea2").value);
            yvmobile.ajax.request("http://kaai.com.ve/labs/yvmobile/post.php", function(responseText, success){
                if (success == true) {
                    alert(responseText + "POST request success!!!");
                }
            }, "POST", parameters);
        }
        
        function getTest(){
            yvmobile.ajax.request("http://kaai.com.ve/labs/yvmobile/get.php?param1=value1", function(responseText, success){
                if (success == true) {
                    alert(responseText + "GET request success!!!");
                }
            }, "GET", null);
        }
    }
    else {
        alert("yvmobile can not be initialized");
    }
</script>
<script type='text/javascript'>
    // If yvmobile has been initialize
    function get(){
        // Send de JSON Encode to the OnOffLayer
        parameters = [{
            url: "http://RESTresource"
        }, {
            tableName: "Table Example Name"
        }, {
            params: [{
                param: "value1"
            }, {
                param: "value2"
            }, {
                param: "value3"
            }]
        }];
        /*alert(parameters.tableName);
         alert(parameters.params[1]);*/
        sendData(parameters);
        /**
         *
         * @param {Object} parameters A json Object that include the url the parameters name and value example
         *
         *
         */
        function sendData(parameters){
            if (yvmobile) {
                // How this layer should work//
                // This layer mus be and abstraction so the application Not know how eather where the data is going to be saved.
                //the data receive a json that could have the table as a first parameter and the data and column to insert as a second parameter
                yvmobile.logger.info('OnOff Layer');
                
                
                yvmobile.logger.info('OnOff Layer State 1');
                // If has offline suport
                if (yvmobile.offline.hasOfflineSupport()) {
                    yvmobile.logger.info('We have offline support');
                    
                    // Insert data locally
                    // table , params + values
                    yvmobile.sql.exec('');
                }
                else {
                    yvmobile.logger.warn('Not offline Support');
                    
                }
                
                yvmobile.logger.info('OnOff Layer State 2');
                // If is online
                
                // parse the json to get the url and parameters
                //for
                
                // We must parse the json and put it in here
                parameters = "mytextarea1=" + encodeURI(document.getElementById("mytextarea1").value) +
                "&mytextarea2=" +
                encodeURI(document.getElementById("mytextarea2").value);
                
                if (yvmobile.offline.isOnline()) {
                
                    yvmobile.logger.info('We are Online');
                    yvmobile.ajax.request("post.php", function(responseText, success){
                        if (success == true) {
                            yvmobile.logger.info(responseText + "Post request success!!!");
                        }
                    }, "POST", parameters);
                }
                else {
                    yvmobile.logger.info('We are Online');
                }
                
                yvmobile.logger.info('OnOff Layer State 3');
                // If is NOT online and NOT offline support
                if (!yvmobile.offline.isOnline() && !yvmobile.offline.hasOfflineSupport()) {
                    yvmobile.logger.error('We are Not Online and We dont have Offline support');
                    
                }
            }
        }
        
        /**
         * - Escenarios al hacer tratar modificar informacion(CRUD)
         * Soporte de Gears sin conexion: realiza el insert de los datos localmente.
         * Soporte de Gears con conexion: realiza el insert local y posteriormente el proceso de sincronizacion realiza la subida de datos
         * No soporta Gears: intenta realizar el post via ajax al servidor remoto.
         * Debemos decidir cada cuanto se realizara la sincronizacion del telefono con el servidor
         */
        /**
         * - Estrategia de Sincronizacion
         *
         */
    }
</script>
